package org.ftc.dao;

import java.util.Iterator;
import java.util.List;

import org.ftc.domain.InCharge;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class InChargeDao extends CurrentDao <InCharge, String> {

	@Override
	public InCharge load(String id) {
		return super.load(id);
	}

	@Override
	public InCharge get(String id) {
		return super.get(id);
	}

	@Override
	public synchronized boolean save(InCharge entity) {
		return super.save(entity);
	}

	@Override
	public synchronized boolean saveOrUpdate(InCharge entity) {
		return super.saveOrUpdate(entity);
	}

	@Override
	public synchronized boolean update(InCharge entity) {
		return super.update(entity);
	}

	@Override
	public synchronized boolean delete(InCharge entity) {
		return super.delete(entity);
	}

	@Override
	public synchronized boolean delete(String id) {
		return super.delete(id);
	}
	
	@Override
	public List<InCharge> findPage(int offset, int pageSize) {
		return super.findPage(offset, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InCharge> findAll() {
		try{	
			Criteria criteria = getSession().createCriteria(InCharge.class);
			criteria.setCacheable(true);
			criteria.addOrder(Order.asc("telNo"));
			List<InCharge> list = (List<InCharge>) criteria.list();
			return list; 
		}catch( Exception e ){
			e.printStackTrace();
			getLog().error(e.toString());
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public InCharge findByUserId(String userId){
		try{	
			Criteria criteria = getSession().createCriteria(InCharge.class);
			criteria.add( Restrictions.eq("userId", userId ) );
			criteria.setCacheable(true);
			List<InCharge> list =  criteria.list();
			for(Iterator<InCharge> it =list.iterator(); it.hasNext(); )
				return (InCharge)it.next(); 
			return null;  
		}catch( Exception e ){
			return null;
		}
    }

	@Override
	public long countAll() {
		return super.countAll();
	}


}
